# -*- coding: utf-8 -*-
# @Time:2021/04/27 21:48
# @Author:Cindy
from time import sleep

from appium.webdriver.common.mobileby import MobileBy
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

from pages.add_member_page import AddMemberPage
from pages.base_page import BasePage
from pages.member_info import MemberInfoPage


class ContactPage(BasePage):

    def goto_add_member_page(self):
        # 点击[添加成员]按钮
        # add_member_button = (MobileBy.XPATH, "//*[@text='添加成员']")
        # self.find(add_member_button).click()
        # uiautomator的定位方式
        # self.driver.find_element(MobileBy.ANDROID_UIAUTOMATOR,
        #                          'new UiScrollable(new UiSelector()'
        #                          '.scrollable(true).instance(0)).'
        #                          'scrollIntoView(new UiSelector().'
        #                          'text("添加成员").instance(0));').click()
        add_member_button = (MobileBy.XPATH, f"//*[@text='添加成员']")
        self.swip_find(add_member_button)
        # 进入添加成员页面
        return AddMemberPage(self.driver)

    def goto_member_info_page(self, name):
        # 点击要删除的成员姓名
        member_button = (MobileBy.XPATH, f"//*[@resource-id ='com.tencent.wework:id/dyi']//*[@text='{name}']")
        self.swip_find(member_button)
        return MemberInfoPage(self.driver)

    def get_memberlist(self):
        sleep(3)
        # 加入显示等待，等到[通讯录]可点击时，才可获取通讯录成员列表
        contact_button = (MobileBy.XPATH, "//*[@text='通讯录']")
        WebDriverWait(self.driver, 10).until(expected_conditions.element_to_be_clickable(contact_button))
        # 获取成员列表
        datas = self.driver.find_elements(MobileBy.CLASS_NAME, "android.widget.TextView")
        memberlist = [data.text for data in datas]
        print(memberlist)
        return memberlist
